package models;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by yangxianming on 2017/5/5.
 *
 */
public class MyFile {
    private String projectPath = System.getProperty("user.dir");
    FileOutputStream fileOutputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    File file = null;

    public MyFile(String logNameFormat){
        SimpleDateFormat sdf1 = new SimpleDateFormat(logNameFormat);
        String logName = sdf1.format(new Date());
        this.file = new File(projectPath + "/logs/threadLog/" + logName);
        try {
            this.fileOutputStream = new FileOutputStream(file, true);
            this.outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public MyFile(){}

    public void writeThreadLog(String info){
//        SimpleDateFormat sdf1 = new SimpleDateFormat(logNameFormat);
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
//        String logName = sdf1.format(new Date());
        String logHeader = sdf2.format(new Date());
//        File file = new File(projectPath + "/logs/threadLog/" + logName);
//        FileOutputStream fileOutputStream = null;
//        OutputStreamWriter outputStreamWriter = null;
        try {
//            fileOutputStream = new FileOutputStream(file, true);
//            outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
            outputStreamWriter.write(logHeader + " " + info + "\n");
            outputStreamWriter.flush();
        }catch (Exception e){
            System.out.println("writing log exception!!");
            e.printStackTrace();
        }
    }

    public void writeLog(String info){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
        String logName = sdf1.format(new Date());
        String logHeader = sdf2.format(new Date());
        File file = new File(projectPath + "/logs/" + logName);
        FileOutputStream fileOutputStream = null;
        OutputStreamWriter outputStreamWriter = null;
//        System.out.println("inner-----" + info);
        try {
            fileOutputStream = new FileOutputStream(file, true);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
            outputStreamWriter.write(logHeader + " " + info + "\n");
        }catch (Exception e){
            System.out.println("writing log exception!!");
            e.printStackTrace();
        }finally {
            if (outputStreamWriter != null){
                try {
                    outputStreamWriter.flush();
                    outputStreamWriter.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    public void writeString(String fillPath, String string){
        writeFile(fillPath, string, true);
    }

    public void writeTxt(String info){
        /*
        {"data":[
            {"api":"/yxm1/fdfdf/haha","phone":"12123123","name":"老杨"},
            {"api":"/yxm2/fdfdf/haha","phone":"4343434343","name":"老杨1"},
            {"api":"/yxm3/fdfdf/haha","phone":"67676767676","name":"老杨2"}
        ]}
         */
        String s = "";
        JSONObject jsonObject = JSON.parseObject(info);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        for (Object o: jsonArray){
            JSONObject jsonObject1 = (JSONObject)o;
            s = s + jsonObject1.getString("api") + " " + jsonObject1.getString("phone") + " " +jsonObject1.getString("name") + "\n";
        }
        s = s.trim();
        System.out.println(s);
        String fillPath = projectPath + "conf/apiAndPhone";
        writeFile(fillPath, s, false);
    }

    //返回文件最后修改时间
    public long fileLastChange(String fillFileName){
        return new File(fillFileName).lastModified();
//        File file = new File(fillFileName);
//        return file.lastModified();
    }

    public ArrayList<String[]> readTxt(String fileName){
        //[{"apiPaths":["task","compare"],"method":"post","phone":"13444444444"}, ...]
        ArrayList<String[]> apiAndPhone = new ArrayList<String[]>();
        File file = new File(projectPath + "conf/" + fileName);
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null){
                apiAndPhone.add(line.split("\\s+"));
            }
            bufferedReader.close();
            return apiAndPhone;
        }catch (Exception e){
            System.out.println("reading log exception!!");
//            e.printStackTrace();
        }finally {
            if (bufferedReader != null){
                try {
                    bufferedReader.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return apiAndPhone;
    }

    public ArrayList<String> readLogGetList(String fileName){
        ArrayList<String> logInfo = new ArrayList<String>();
        File file = new File(fileName);
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null){
                logInfo.add(line);
            }
            bufferedReader.close();
        }catch (Exception e){
//            System.out.println("reading log exception!!");
            logInfo.add("read log exception!!");
//            e.printStackTrace();
        }finally {
            if (bufferedReader != null){
                try {
                    bufferedReader.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return logInfo;
    }

    public JSONArray readLog(String fileName){
        JSONArray logInfo = new JSONArray(); //保存日志内容
        File file = new File(fileName);
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null){
                logInfo.add(line);
            }
            bufferedReader.close();
        }catch (Exception e){
//            System.out.println("reading log exception!!");
            logInfo.add("read log exception!!");
//            e.printStackTrace();
        }finally {
            if (bufferedReader != null){
                try {
                    bufferedReader.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        }
        return logInfo;
    }

    private void writeFile(String fillPath, String info, boolean isAdd){
        File file = new File(fillPath);
        FileOutputStream fileOutputStream = null;
        OutputStreamWriter outputStreamWriter = null;
//        System.out.println("inner-----" + info);
        try {
            fileOutputStream = new FileOutputStream(file, isAdd);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
            outputStreamWriter.write(info);
        }catch (Exception e){
            System.out.println("writing log exception!!");
//            e.printStackTrace();
        }finally {
            if (outputStreamWriter != null){
                try {
                    outputStreamWriter.flush();
                    outputStreamWriter.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
